package fr.univ.surveyonline.tags;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import fr.univ.surveyonline.datastore.PMF;
import fr.univ.surveyonline.model.User;
import fr.univ.surveyonline.model.allEnum.Role;

public class AllUser extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException	{
		
		List<User> list = (List<User>) PMF.getAllFromClass(User.class);

		Set<User> membres = new HashSet<User>();
		Set<User> sondeurs = new HashSet<User>();
		
		for (User u : list) {
			if (u.getRole() == Role.SONDEUR) {
				sondeurs.add(u);
			} else {
				membres.add(u);
			}
		}
		
		JspWriter out = pageContext.getOut();
		try {
			out.println("<p> Sondeurs :");
			out.println("<ul>");
			for (User u : sondeurs) {
				out.println("<li><a href=\"/connect?id="+u.getKey().getId()+"\">" + u.getLogin() + "</a></li>");
			}
			out.println("</ul>");
			out.println("</p>");
			out.println("<p> Membres :");
			out.println("<ul>");
			for (User u : membres) {
				out.println("<li><a href=\"/connect?id="+u.getKey().getId()+"\">" + u.getLogin() + "</a></li>");
			}
			out.println("</ul>");
			out.println("</p>");

		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}
